import RequestProcess from "./RequestProcess";
import operator from "../operator/Operator";
import OperateArgs from "../operator/OperateArgs";
import OperatorDecorator from "../operator/OperatorDecorator";
import CheckGroupNameExistOperator from "../operator/CheckGroupNameExistOperator";
import GenerateIdOperator from "../operator/GenerateIdOperator";
import CreateGroupObjectOperator from "../operator/CreateGroupObjectOperator"
import CreateGroupMessageBoxOperator from "../operator/CreateGroupMessageBoxOperator";
import EnterGroupOperator from "../operator/EnterGroupOperator";
import { RC } from "../config/RC";


class CreateGroupRequestProcess extends RequestProcess {

    public args_generate(): void {
        this.args = new OperateArgs();
        this.args.setProperty("userId", this.req.body.userId);
        this.args.setProperty("groupName", this.req.body.groupName);
    }

    public operator_plan_generate(): void {
        const check_group_name_exist_operator = new CheckGroupNameExistOperator(this.args);
        const generate_id_operator = new GenerateIdOperator();
        const create_group_object_operator = new CreateGroupObjectOperator();
        const enter_group_operator = new EnterGroupOperator();
        const create_group_messagebox_operator = new CreateGroupMessageBoxOperator();

        this.operator_plan = new OperatorDecorator(generate_id_operator, check_group_name_exist_operator);
        this.operator_plan = new OperatorDecorator(create_group_object_operator, this.operator_plan);
        this.operator_plan = new OperatorDecorator(enter_group_operator, this.operator_plan);
        this.operator_plan = new OperatorDecorator(create_group_messagebox_operator, this.operator_plan);

    }

    public writeResult(rc: RC): Object {
        switch (rc) {
            case RC.SUCCESS:
                return {
                    code: "0000",
                    msg: "创建群组成功",
                    groupId: this.getOperateArgs().getProperty("generateId")
                }
            case RC.GROUPNAME_EXIST:
                return {
                    code: "1007",
                    msg: "群组名已存在"
                }
            case RC.INVALID_ARGUMENT:
                return {
                    code: "1002",
                    msg: "参数不合法"
                }
            case RC.INTERNAL_ERROR:
                return {
                    code: "1003",
                    msg: "服务器内部错误"
                }
            default:
                return {
                    code: "2000",
                    msg: "未知错误"
                }
        }
    }
}

export default CreateGroupRequestProcess;